#include <algorithm>
#include <stdexcept>
#include "ExamWindow.hpp"
#include "ChoiceForm.hpp"
#include "ui_ChoiceForm.h"
#include <QMessageBox>
#include <QLabel>

//const QString huida_qstr=QString::fromUtf8("\u56DE\u7B54");
//const QString zhengque_qstr=QString::fromUtf8("\u6B63\u786E");
//const QString cuowu_qstr=QString::fromUtf8("\u9519\u8BEF");
const QString daanwei_qstr=QString::fromUtf8("\u6B63\u786E\u7B54\u6848\u4E3A\uFF1A");

ChoiceForm::ChoiceForm(RandomQuestion *rdq, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ChoiceForm),
    randomq(rdq),
    itemfont(NULL)
{
    ui->setupUi(this);
    ui->textBrowser->setReadOnly(true);
    status=false;
}

ChoiceForm::~ChoiceForm()
{
    delete ui;
}

void ChoiceForm::loadquestion(const ChoiceQuestion *q)
{
    ui->textBrowser->setPlainText(*q->content);
    QLayout *layout=ui->scrollAreaWidgetContents->layout();
    if (layout==NULL){
        layout=new QVBoxLayout(ui->scrollAreaWidgetContents);
        ui->scrollAreaWidgetContents->setLayout(layout);
    }
    for (unsigned i=0;i<prevWidgets.size();i++){
        const VisibleItem &item=prevWidgets.at(i);
        layout->removeWidget(item.label);
        item.label->hide();
    }
    prevWidgets.clear();
    unsigned answer=q->answer;
    for (unsigned i=0;i<q->choices.size();i++){
        VisibleItem item={NULL,
                         q->choices.at(i),answer & 1};
        if (backups.empty()){
            item.label=new QLabel(ui->scrollAreaWidgetContents);
        }else{
            item.label=backups.top();
            backups.pop();
            item.label->show();
        }

        if (itemfont){
            item.label->setFont(*itemfont);
        }
        answer >>=1;
        prevWidgets.push_back(item);
    }
    std::random_shuffle(prevWidgets.begin(),prevWidgets.end(),randomq->device());
    char prefix[]={"A: "};
    for (unsigned i=0;i<prevWidgets.size();i++){
        QString text;
        text.append(prefix);
        prefix[0]++;
        text.append(prevWidgets.at(i).content);
        prevWidgets.at(i).label->setText(text);
        layout->addWidget(prevWidgets.at(i).label);
    }
}

bool ChoiceForm::checkCorrection(QString *msg)
{
    if (msg){
        msg->clear();
    }
    char buffer[64],*pbuf=buffer;
    for (unsigned i=0;i<prevWidgets.size();i++){
        const VisibleItem &item=prevWidgets.at(i);
        if(item.answer){
            *pbuf++='A'+i;
        }
    }
    *pbuf=0;
    if (msg){
        //msg->append(huida_qstr);
        //msg->append(ret?zhengque_qstr:cuowu_qstr);
        msg->append(daanwei_qstr);
        msg->append(buffer);
    }
    status=false;
    return true;
}

bool ChoiceForm::nextQuestion(QString *msg)
{
    PStaticChoiceQuestion scq=randomq->randomChooseChoiceQuestion();
    if (!scq.question){
        if (msg){
            *msg=QString::fromUtf8("\u9009\u62E9\u9898\u5DF2\u5168\u90E8"
                                   "\u7528\u5B8C\uFF0C\u8BF7\u6DFB\u52A0\u65B0\u9898\u5E93");
        }
        return status=false;
    }
    ChoiceQuestion q;
    q.content=&(scq.question->content);
    q.answer=scq.question->answer;
    q.multichoice=(q.answer&(q.answer-1));
    q.id=scq.question->id;
    for (int i=0;i<scq.question->options.size();i++){
        q.choices.push_back(QStringRef(&scq.question->options.at(i)));
    }
    loadquestion(&q);
    if (msg){
        *msg=QString::fromUtf8("\u9898\u76EEID\uFF1A%1").arg(q.id);
    }
    return status=true;
}

void ChoiceForm::updateConfig(const ExamConfig *cfg)
{
    //ui->mainTitle->setFont(cfg->itemfont);
    //ui->subTitle->setFont(cfg->itemfont);
    ui->textBrowser->setFont(cfg->mainfont);
    itemfont=&cfg->itemfont;
    for(unsigned i=0;i<prevWidgets.size();i++){
        prevWidgets.at(i).label->setFont(cfg->itemfont);
    }
}

void ChoiceForm::clearText()
{
    if(!status){
        ui->textBrowser->clear();
        QLayout *layout=ui->scrollAreaWidgetContents->layout();
        if (layout==NULL){
            layout=new QVBoxLayout(ui->scrollAreaWidgetContents);
            ui->scrollAreaWidgetContents->setLayout(layout);
        }
        for (unsigned i=0;i<prevWidgets.size();i++){
            const VisibleItem &item=prevWidgets.at(i);
            layout->removeWidget(item.label);
            item.label->hide();
        }
        prevWidgets.clear();
    }
}
